package com.xiaoyun.rollutil.config;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.file.FileReader;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.json.JSONUtil;
import com.xiaoyun.rollutil.entity.bo.CityBO;
import com.xiaoyun.rollutil.entity.bo.DistrictBO;
import com.xiaoyun.rollutil.entity.bo.ProvinceBO;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;
import org.springframework.util.ResourceUtils;

import java.io.File;
import java.io.FileNotFoundException;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @Author: XiaoYun461
 * @Date: 2021/12/2 19:52
 */
@Component
public class AddressMap implements ApplicationListener<ContextRefreshedEvent> {
    /**
     * key 为  	省/市/区编号
     * value 为  省/市/区名称
     */
    private static final Map<String, String> NAME_MAP = new HashMap<>();
    /**
     * key 为 省/市/区编号
     * value 为 其父类的 编号 当 value为0时, 为省,顶级分类
     */
    private static final Map<String, String> ID_MAP = new HashMap<>();


    private static final String CITY_AREA = "市辖区";


    public final String getCityNameByCode(String code) {
        String cityName = NAME_MAP.get(code);
        if (!Objects.equals(cityName, CITY_AREA)) {
            return cityName;
        }
        return getCityNameByCode(ID_MAP.get(code));

    }

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        try {
            File file = ResourceUtils.getFile("classpath:city.json");
            FileReader fileReader = FileReader.create(file, Charset.forName("utf-8"));
            String jsonStr = fileReader.readString();
            List<ProvinceBO> provinceBos = JSONUtil.toList(jsonStr, ProvinceBO.class);
            for (ProvinceBO provinceBO : provinceBos) {
                String code = provinceBO.getCode();
                String name = provinceBO.getName();
                NAME_MAP.put(code, name);
                ID_MAP.put(code, "0");
                List<CityBO> pchilds = provinceBO.getPchilds();
                if (CollUtil.isEmpty(pchilds)) {
                    continue;
                }
                for (CityBO pchild : pchilds) {
                    String pchildCode = pchild.getCode();
                    String pchildName = pchild.getName();
                    NAME_MAP.put(pchildCode, pchildName);
                    ID_MAP.put(pchildCode, code);
                    List<DistrictBO> cchilds = pchild.getCchilds();
                    if (CollUtil.isEmpty(cchilds)) {
                        continue;
                    }
                    for (DistrictBO cchild : cchilds) {
                        String cchildCode = cchild.getCode();
                        String cchildName = cchild.getName();
                        NAME_MAP.put(cchildCode, cchildName);
                        ID_MAP.put(cchildCode, pchildCode);
                    }
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
}
